import pygame,sys,random,time
pygame.init()
yunxing=True
def playponggame3(pygame,random,time,yunxing):
    while yunxing:
        def shua(pygame):
            pygame.display.flip()
        w=[600,800]
        x=[0,30]
        y,c=[0,20],[255,0,0]
        x2=50
        def hui(fp,fen):
            f=open(fp,"w+")
            s=str(fen[0])
            s1=str(fen[1])
            s2=str(fen[2])
            s3=str(fen[3])
            s4=str(fen[4])
            f.writelines([s+"\n",s1+"\n",s2+"\n",s3+"\n",s4+"\n"])
        fp="python3/study/pong.game2.txt"
        f=open(fp,"r+")
        oldfen=int(f.readline(100))
        fen1=int(f.readline(100))
        fen2=int(f.readline(100))
        fen3=int(f.readline(100))
        fencha=int(f.readline(100))
        min=0
        def tulifen(fen):
            oldfen=fen[0]
            fen1=fen[1]
            fen2=fen[2]
            fen3=fen[3]
            fencha=fen[4]
            xx=True
            while xx:
                if oldfen<fen3:
                    fen[5]=int((fen3-oldfen)/fencha)
                    xx=False
                if oldfen>fen3:
                    if oldfen>fen2:
                        if oldfen>fen1:
                            fen[5]=1
                            xx=False
                        else:
                            fen[5]=2
                            xx=False
                    else:
                        fen[5]=3
                        xx=False
            fen=[oldfen,fen1,fen2,fen3,fencha,min]
        fen=[oldfen,fen1,fen2,fen3,fencha,min]
        tulifen(fen)
        ball=[random.randint(100,500),random.randint(100,500),40,40,False,3,0,0,0,0]
        screen=pygame.display.set_mode(w)
        screen.fill((255,255,255))
        shua(pygame)
        def ballzhuang(ball,x):
            if ball[0]<=10:
                ball[2]=-(ball[2])
            if ball[0]>=590:
                ball[2]=-(ball[2])
            if ball[1]<=10:
                ball[3]=-(ball[3])
            if ball[1]>770:
                if ball[0]<x-1000 or ball[0]>x+1000:#70
                    ball[4]=True
                    ball[5]-=1
                    ball[8]+=1
                elif ball[7]==0:
                    ball[3]=-(ball[3])
                    ball[6]+=(-(ball[3])/2)*ball[5]*ball[5]
                    ball[9]+=1
                ball[7]=7
            if ball[7]>0:
                ball[7]-=1
            ball[0]+=ball[2]
            ball[1]+=ball[3]
            return ball
        def drawflip(screen,c,x,y):
            pygame.draw.rect(screen,c,[int(x[0]),int(y[0]),int(x[1]-x[0]),int(y[1]-y[0])],0)       
        def draw(screen,x,y,x1,y1,c):
            drawflip(screen,c,x,y)
            if x1>x[0] and x1<x[1]:
                if y1>y[0] and y1<y[1]:
                    return True
                else:
                    return False
            else:
                return False
        def pygameprint(screen,p,pygame,daxiao,c,weizhi):
            score=str(p)
            font = pygame.font.Font(None, daxiao)
            my_score = font.render(str(score), False, c)
            screen.blit(my_score, (weizhi[0], weizhi[1])) 
            shua(pygame)
        def drawlines(pygame,screen):
            pygame.draw.lines(screen,[255,255,255],True,[[int(x[0]),int(y[0])],[int(x[1]),int(y[1])]],2)
            pygame.draw.lines(screen,[255,255,255],True,[[int(x[1]),int(y[0])],[int(x[0]),int(y[1])]],2)
        drawflip(screen,c,x,y)
        drawlines(pygame,screen)
        shua(pygame)
        def drawfen(pygame,screen,fen):
            pygame.draw.rect(screen,[155,155,155],[150,600,300,150],0)
            pygame.draw.rect(screen,[55,55,55],[150,600,300,30],1)
            pygame.draw.rect(screen,[55,55,55],[150,630,300,30],1) 
            pygame.draw.rect(screen,[55,55,55],[150,660,300,30],1) 
            pygame.draw.rect(screen,[55,55,55],[150,690,300,30],1)
            pygame.draw.rect(screen,[55,55,55],[150,720,300,30],1)
            x=0
            while x<4:
                font = pygame.font.Font(None, 50)
                if x<3:
                    screen.blit(font.render(str(x+1), False, [0,0,0]), (150,600+x*30))
                if x==3:
                    screen.blit(font.render(str(fen[5]), False, [0,0,0]), (150,630+x*30))
                x+=1
            p=[fen[0],fen[1],fen[2],fen[3]]
            p.sort()
            #print(p)
            my_score = font.render(str(p[3]), False, [0,0,0])
            screen.blit(my_score, (275,600+0*30))
            my_score = font.render(str(p[2]), False, [0,0,0])
            screen.blit(my_score, (275,630+0*30))
            my_score = font.render(str(p[1]), False, [0,0,0])
            screen.blit(my_score, (275,660+0*30))
            my_score = font.render(str(fen[0]), False, [0,0,0])
            screen.blit(my_score, (275,720+0*30))
            shua(pygame)
        def w1(screen,pygame,w,fen,yunxing):
            
            z1=[200,400,500,550]
            c=[255,0,0]
            drawflip(screen,c,[z1[0],z1[1]],[z1[2],z1[3]])
            pygameprint(screen,"play",pygame,60,[255,255,255],[z1[0]+50,z1[2]])
            drawfen(pygame,screen,fen)
            shua(pygame)
            r=True
            while r:
                for event in pygame.event.get():
                    if  event.type == pygame.MOUSEBUTTONUP:
                        if event.button==1:
                            x1,y1=event.pos
                            if draw(screen,x,y,x1,y1,c):
                                r=False
                                yunxing=False
                                
                                #pygame.quit()
                            elif draw(screen,[z1[0],z1[1]],[z1[2],z1[3]],x1,y1,c):
                                r=False

                            else:
                                drawflip(screen,c,[z1[0],z1[1]],[z1[2],z1[3]])
                                drawflip(screen,c,x,y)
                                drawlines(pygame,screen)
                                pygameprint(screen,"play",pygame,60,[255,255,255],[z1[0]+50,z1[2]])
                shua(pygame)
            pygame.draw.rect(screen,[255,255,255],[0,0,int(w[0]),int(w[1])],0) 
            drawflip(screen,c,x,y)
            drawlines(pygame,screen)
            shua(pygame)
            if yunxing==False:
                pygame.quit
        w1(screen,pygame,w,fen,yunxing)
        clock=pygame.time.Clock()
        r=True
        while r:
            for event in pygame.event.get():
                if  event.type == pygame.MOUSEBUTTONUP:
                    if event.button==1:
                        x1,y1=event.pos
                        if draw(screen,x,y,x1,y1,c):
                            #yunxing=False
                            fen[0]+=int(ball[6])
                            r=False
            screen.fill((255,255,255))
            x2,y2 = pygame.mouse.get_pos()
            drawflip(screen,c,[0,30],[0,20])
            drawlines(pygame,screen)
            ball=ballzhuang(ball,x2)
            pygame.draw.rect(screen,[0,255,0],[x2-100,780,200,20],0)#x2-70,140
            pygame.draw.circle(screen, [255,0,0], [ball[0],ball[1]], 10)
            font = pygame.font.Font(None, 60)
            my_score = font.render("score:", True, c)
            screen.blit(my_score, (350, 0))
            score=int(ball[6])
            my_score = font.render(str(score), True, c)
            screen.blit(my_score, (500, 0))
            weizhi=ball[5]
            w=350
            while weizhi>0:
                pygame.draw.circle(screen,[255,0,0],[w+10,50],10)
                w+=30
                weizhi-=1
            while ball[4]:
                if ball[5]>0:
                    drawflip(screen,c,[200,400],[500,550])
                    drawflip(screen,c,x,y)
                    drawlines(pygame,screen)
                    pygameprint(screen,"play",pygame,60,[255,255,255],[250,500])
                    pygame.display.update()
                    for event in pygame.event.get():
                        if  event.type == pygame.MOUSEBUTTONUP:
                            if event.button==1:
                                x1,y1=event.pos
                                if draw(screen,[200,400],[500,550],x1,y1,c):
                                    ball[4]=False
                                    ball[1]=random.randint(100,500)
                                    ball[0]=random.randint(100,500)
                                    pygame.draw.circle(screen, [255,0,0], [ball[0],ball[1]], 10)
                                    pygame.display.update()
                                    time.sleep(0.5)
                                if draw(screen,x,y,x1,y1,c):
                                    #yunxing=False
                                    #pygame.quit()
                                    r=False
                                    #fen[0]+=ball[6]
                if ball[5]<=0:
                    screen.fill((255,255,255))
                    drawflip(screen,c,x,y)
                    drawlines(pygame,screen)
                    my_score = font.render("game.over", True, c)
                    screen.blit(my_score, (200, 450))
                    pygame.display.update()
                    time.sleep(2)
                    fen[0]+=int(ball[6])
                    tulifen(fen)
                    w1(screen,pygame,[600,800],fen,yunxing)
                    ball=[random.randint(100,500),random.randint(100,500),4,4,False,3,0,0,0,0]
                drawflip(screen,c,[200,400],[500,550])
                drawflip(screen,c,x,y)
            if ball[9]>=5:
                ball[5]+=1
                ball[9]-=5
                if ball[3]<10 and ball[3]>-10:#10
                    if ball[3]>0:
                        ball[3]+=2
                    if ball[3]<0:
                        ball[3]-=2
                    if ball[2]>0:
                        ball[2]+=2
                    if ball[2]<0:
                        ball[2]-=2
            pygame.display.update() 
            clock.tick(30)#50
    fen[0]+=int(ball[6])
    hui(fp,fen)
    f.close()
playponggame3(pygame,random,time,yunxing)

pygame.quit()